const UserModel = require("../models/user.model");
const BaseController = require("./base.controller");
const { generateToken } = require("../utils/jwt");

class UserController extends BaseController {
  // 注册用户
  static async registerUser(ctx) {
    const { phone, code, type } = ctx.request.body;
    if (!phone) {
      return (ctx.body = BaseController.renderJsonError("手机号不能为空"));
    }
    const isUser = await UserModel.findOne({ where: { phone } });
    if (isUser) {
      ctx.body = BaseController.renderJsonError("用户已存在");
      return;
    }
    const result = await UserModel.create({ phone, code, type }, { raw: true });

    const params = {
      id: result.id,
    };
    ctx.body = BaseController.renderJsonSuccess(generateToken(params));
  }

  // 登录用户
  static async loginInUser(ctx) {
    const { phone, password } = ctx.request.body;
    if (!phone) {
      ctx.body = BaseController.renderJsonError("请输入手机号");
      return;
    }
    const result = await UserModel.findOne({
      where: { phone },
      // attributes: ["id", "phone", "openId"],
      raw: true,
    });
    if (!result) {
      ctx.body = BaseController.renderJsonError("该用户未注册");
      return;
    }
    if (password !== "8888") {
      return (ctx.body = BaseController.renderJsonError("密码错误"));
    }
    const token = generateToken({
      id: result.id,
    });
    ctx.body = BaseController.renderJsonSuccess(token);
  }

  // 获取员工列表
  static async getStaffList(ctx) {
    const result = await UserModel.findAll()
    ctx.body = BaseController.renderJsonSuccess(result);
  }

  // 创建员工
  static async createUser(ctx) {
    const { phone, name, gender, role, status, avatar } = ctx.request.body;
    if (!phone) {
      return (ctx.body = BaseController.renderJsonError("手机号不能为空"));
    }
    const isUser = await UserModel.findOne({ where: { phone } });
    if (isUser) {
      ctx.body = BaseController.renderJsonError("用户已存在");
      return;
    }
    const result = await UserModel.create(
      { phone, name, gender, role, status, avatar },
      { raw: true }
    );

    const params = {
      id: result.id,
    };
    ctx.body = BaseController.renderJsonSuccess(true);
  }

  // 获取用户详情
  static async getUserDetail(ctx) {
    const { id } = ctx?.state?.user || {};
    const result = await UserModel.findOne({
      where: { id },
      // attributes: [
      //   "id",
      //   "phone",
      //   "name",
      //   "openId",
      //   "avatar",
      //   "gender",
      //   "birthday",
      // ],
      raw: true,
    });
    ctx.body = BaseController.renderJsonSuccess(result);
  }

  // 更新用户信息
  static async updateUserInfo(ctx) {
    const { id } = ctx.state.user;
    const { avatar, gender, birthday, name, openId } = ctx.request.body;
    await UserModel.update(
      {
        avatar,
        gender,
        birthday,
        name,
        openId,
      },
      {
        where: { id },
      }
    );
    ctx.body = BaseController.renderJsonSuccess(true);
  }
}
module.exports = UserController;
